package com.lkx.web;

import com.lkx.entity.User;
import com.lkx.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpSession;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * ClassName: UserController
 * Function:  TODO
 * Date:      2018-12-17 12:41
 * author     黄曦
 * version    V1.0
 */
@Controller
@RequestMapping("/user")
public class UserController {

    @Autowired
    private UserService userService;

    @RequestMapping("list")
    public ModelAndView list() {
        ModelAndView view = new ModelAndView("user/list");
        view.addObject("userlist",userService.list(null));
        return view;
    }


    @RequestMapping("add")
    public ModelAndView add(User user) {
        ModelAndView view = new ModelAndView("user/list");
        view.addObject("userlist",userService.insert(user));
        return view;
    }

    /**
     * 到登录页面
     * @param user
     * @return
     */
    @RequestMapping("toLogin")
    public ModelAndView toLogin() {
        ModelAndView view = new ModelAndView("user/login");
        return view;
    }

    /**
     * 登录
     * @param user
     * @return
     */
    @RequestMapping("login")
    public String login(String username, String password, HttpSession session) {
        Map<String,Object> map = new HashMap<>();
        map.put("username",username);
        map.put("password",password);
        User user = userService.list(map).get(0);
        if(user != null){
            session.setAttribute("username",user.getUsername());
            session.setAttribute("user",user);
        }
        return "redirect:/";
    }

    /**
     * 到普通注册页面
     * @param user
     * @return
     */
    @RequestMapping("toRegister")
    public ModelAndView toRegister() {
        ModelAndView view = new ModelAndView("user/add");
        return view;
    }

    /**
     * 到管理员注册页面
     * @param user
     * @return
     */
    @RequestMapping("toAdminRegister")
    public ModelAndView toAdminRegister() {
        ModelAndView view = new ModelAndView("user/admin_register");
        return view;
    }

    /**
     * 注册
     * @param user
     * @return
     */
    @RequestMapping("register")
    public ModelAndView register(User user,HttpSession session) {

        user.setGmtCreate(new Date());
        user.setGmtModify(new Date());
        userService.insert(user);


        ModelAndView view = new ModelAndView();
        session.setAttribute("username",user.getUsername());
        session.setAttribute("user",user);
        view.addObject("username",user.getUsername());
        view.setViewName("redirect:/");
        return view;
    }

    /**
     * 注销登录
     * @param
     * @return
     */
    @RequestMapping("logout")
    public ModelAndView logout(HttpSession session) {
        ModelAndView view = new ModelAndView();
        session.setAttribute("username",null);
        session.setAttribute("user",null);
        view.setViewName("redirect:/");
        return view;
    }


}
